﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;

namespace ViewMaker.Core.Controls
{
    /// <summary>
    /// スタックパネルViewコントロール
    /// </summary>
    public class StackPanelViewControl : PanelViewControl
    {
        /// <summary>
        /// プロパティ定数
        /// </summary>
        public new class Properties : PanelViewControl.Properties
        {
            public const string Orientation = "Orientation";
        }

        /// <summary>
        /// アイテム（子コントロール）の配置方向
        /// </summary>
        [Display(GroupName = LayoutGroup)]
        [View(ViewControlType.ComboBox)]
        [ViewProperty(ComboBoxViewControl.Properties.ItemsSource, "OrientationList")]
        public LayoutOrientation Orientation { get; set; }

        #region ShouldSerialize

        public override bool ShouldSerializeHeaderWidth() { return true; }
        public override bool ShouldSerializeItemWidth() { return true; }

        #endregion


        public StackPanelViewControl()
        {
            ControlType = ViewControlType.StackPanel;
            IsPanel = true;
            Orientation = LayoutOrientation.Vertical;
            HeaderWidth = ViewMakerSettings.Instance.DefaultStackPanelHeaderWidth;
            ItemWidth = ViewMakerSettings.Instance.DefaultStackPanelItemWidth;

        }

        public override List<XamlNode> CreateXaml(XamlNode parent)
        {
            XamlNode panel = CreatePanel(parent, "StackPanel");
            panel.SetAttribute("Orientation", Orientation.ToString());

            foreach (var child in Item.Children)
            {
                var headerItem = child.Element as HeaderViewControl;
                if (headerItem != null)
                {
                    foreach (var item in CreateHeaderControl(child, panel))
                    {
                        panel.AppendChild(item);
                    }
                }
                else
                {
                    foreach (var item in child.CreateXaml(panel))
                    {
                        SetItemAttribute(child, item);
                        panel.AppendChild(item);
                    }
                }
            }
            SetBindingAttribute(panel, "DataContext", Item.BindPath, true);
            ApplyBaseProperty(panel);
            var list = new List<XamlNode>();
            list.Add(Decoration ?? panel);
            return list;
        }


        internal override void CopyFrom(ViewElement source)
        {
            base.CopyFrom(source);
            this.HeaderPosition = LayoutHeaderPosition.Left;
        }
    }

}
